
export const achievement = {
  namespaced: true,
  state: () => ({
    achievements: [],
    unlockedCount: 0
  }),
  mutations: {
    SET_ACHIEVEMENTS(state, payload) {
      state.achievements = payload
    },
    UPDATE_ACHIEVEMENT(state, payload) {
      const index = state.achievements.findIndex(a => a.id === payload.id)
      if (index >= 0) {
        state.achievements.splice(index, 1, payload)
      }
    }
  },
  actions: {
    async fetchAchievements({ commit }) {
      // 调试用默认数据
      const defaultData = [
        // 游戏成就
        {
          id: 1, 
          name: '初出茅庐', 
          type: 'game', 
          progress: 0, 
          target: 1, 
          unlocked: false,
          icon: '/static/achievements/beginner.png',
          description: '完成第一局游戏'
        },
        {
          id: 2, 
          name: '弹珠大师', 
          type: 'game', 
          progress: 0, 
          target: 10, 
          unlocked: false,
          icon: '/static/achievements/master.png',
          description: '赢得10局游戏'
        },
        {
          id: 3, 
          name: '地形专家', 
          type: 'game', 
          progress: 0, 
          target: 5, 
          unlocked: false,
          icon: '/static/achievements/terrain.png',
          description: '在所有地形上获胜'
        },
        {
          id: 4, 
          name: '完美一击', 
          type: 'game', 
          progress: 0, 
          target: 1, 
          unlocked: false,
          icon: '/static/achievements/perfect.png',
          description: '一杆进洞'
        },
        {
          id: 5, 
          name: '弹珠收藏家', 
          type: 'game', 
          progress: 0, 
          target: 20, 
          unlocked: false,
          icon: '/static/achievements/collector.png',
          description: '收集20个弹珠'
        },
        {
          id: 6, 
          name: '连胜王者', 
          type: 'game', 
          progress: 0, 
          target: 5, 
          unlocked: false,
          icon: '/static/achievements/streak.png',
          description: '连续赢得5局游戏'
        },
        {
          id: 7, 
          name: '快速反应', 
          type: 'game', 
          progress: 0, 
          target: 1, 
          unlocked: false,
          icon: '/static/achievements/fast.png',
          description: '在10秒内完成一局'
        },
        
        // 社交成就
        {
          id: 8, 
          name: '社交达人', 
          type: 'social', 
          progress: 0, 
          target: 5, 
          unlocked: false,
          icon: '/static/achievements/social.png',
          description: '邀请5位好友'
        },
        {
          id: 9, 
          name: '团队领袖', 
          type: 'social', 
          progress: 0, 
          target: 3, 
          unlocked: false,
          icon: '/static/achievements/leader.png',
          description: '创建3个游戏房间'
        },
        {
          id: 10, 
          name: '慷慨解囊', 
          type: 'social', 
          progress: 0, 
          target: 5, 
          unlocked: false,
          icon: '/static/achievements/generous.png',
          description: '赠送5个弹珠给好友'
        },
        {
          id: 11, 
          name: '复仇者', 
          type: 'social', 
          progress: 0, 
          target: 3, 
          unlocked: false,
          icon: '/static/achievements/revenge.png',
          description: '击败曾经击败过你的玩家3次'
        },
        {
          id: 12, 
          name: '友谊第一', 
          type: 'social', 
          progress: 0, 
          target: 10, 
          unlocked: false,
          icon: '/static/achievements/friendship.png',
          description: '与10位不同玩家对战'
        },
        
        // 特殊成就
        {
          id: 13, 
          name: '午夜战士', 
          type: 'special', 
          progress: 0, 
          target: 1, 
          unlocked: false,
          icon: '/static/achievements/night.png',
          description: '在午夜玩游戏'
        },
        {
          id: 14, 
          name: '幸运儿', 
          type: 'special', 
          progress: 0, 
          target: 1, 
          unlocked: false,
          icon: '/static/achievements/lucky.png',
          description: '连续3次一杆进洞'
        },
        {
          id: 15, 
          name: '障碍物专家', 
          type: 'special', 
          progress: 0, 
          target: 10, 
          unlocked: false,
          icon: '/static/achievements/obstacle.png',
          description: '利用障碍物赢得10次比赛'
        },
        {
          id: 16, 
          name: '弹珠银行', 
          type: 'special', 
          progress: 0, 
          target: 50, 
          unlocked: false,
          icon: '/static/achievements/bank.png',
          description: '累计获得50个弹珠'
        },
        {
          id: 17, 
          name: '皮肤收藏家', 
          type: 'special', 
          progress: 0, 
          target: 10, 
          unlocked: false,
          icon: '/static/achievements/skin.png',
          description: '收集10种弹珠皮肤'
        },
        {
          id: 18, 
          name: '积分大亨', 
          type: 'special', 
          progress: 0, 
          target: 1000, 
          unlocked: false,
          icon: '/static/achievements/points.png',
          description: '累计获得1000积分'
        },
        {
          id: 19, 
          name: '全能冠军', 
          type: 'special', 
          progress: 0, 
          target: 1, 
          unlocked: false,
          icon: '/static/achievements/champion.png',
          description: '解锁所有游戏成就'
        },
        {
          id: 20, 
          name: '社交之星', 
          type: 'special', 
          progress: 0, 
          target: 1, 
          unlocked: false,
          icon: '/static/achievements/star.png',
          description: '解锁所有社交成就'
        }
      ]
      commit('SET_ACHIEVEMENTS', defaultData)
      return defaultData
    },
    async updateAchievement({ commit }, { achievementId, progress }) {
      // 调试用模拟更新
      const updated = {
        id: achievementId,
        name: '测试成就',
        type: 'game',
        progress: progress,
        target: 10,
        unlocked: progress >= 10
      }
      commit('UPDATE_ACHIEVEMENT', updated)
      return updated
    }
  },
  getters: {
    gameAchievements: state => state.achievements.filter(a => a.type === 'game'),
    socialAchievements: state => state.achievements.filter(a => a.type === 'social'),
    specialAchievements: state => state.achievements.filter(a => a.type === 'special')
  }
}
